"use strict";

const Controller = require("egg").Controller;
const jwt = require("jsonwebtoken");

class LoginController extends Controller {
  //登录接口
  async login() {
    const { app, ctx } = this;

    const { username, password } = ctx.request.body;

    // 判断username 和password是否存在于数据库
    const data = await app.mysql.get("login", { username, password });

    console.log(data);

    if (data) {
      ctx.body = {
        code: 1,
        token: jwt.sign(data.id, "0311"),
      };
    } else {
      ctx.status = 401;
      ctx.body = {
        code: 0,
        msg: "用户名或者密码错误，或者用户未注册",
      };
    }
  }

  //第三方登录
  async giteeLogin() {
    const { ctx, app } = this;
    // 获取前端code码
    const { code } = ctx.request.body;
    // 通过code  后端请求token值
    const res = await ctx.curl("https://gitee.com/oauth/token", {
      method: "POST",
      data: {
        grant_type: "authorization_code",
        code,
        client_id:
          "9af66fcadad19c10be73154261e8da50bd8ad2894d189b9973daf84b6d235058",
        redirect_uri: "http://127.0.0.1:3000/login",
        client_secret:
          "d8648dc26935abc937d4e55995e97c273ad35f381e20f1f178fa6b3fab708fb6",
      },
      //返回值
      dataType: "json",
    });
    // 获取token值
    const access_token = res.data.access_token;
    // 获取用户信息
    const data = await ctx.curl(
      `https://gitee.com/api/v5/user?access_token=${access_token}`,
      { dataType: "json" }
    );
    // 数据库查找有无用户信息
    const userInfo = await app.mysql.get("login", { id: data.data.id });
    if (userInfo) {
      ctx.body = {
        code: 200,
        token: jwt.sign(data.data.id, "0311"),
      };
    } else {
      // 没有添加用户 之后返回token
      await app.mysql.insert("login", {
        username: data.data.name,
        password: "",
        name: data.data.login,
        img: data.data.avatar_url,
        auth: "101",
        id: data.data.id,
      });

      ctx.body = {
        code: 200,
        token: jwt.sign(data.data.id, "0311"),
      };
    }
  }

  //注册
  async register() {
    const { ctx, app } = this;

    const { username, password, name, auth, img = "" } = ctx.request.body;

    // 判断username 是否存在于我的数据库中
    const data = await app.mysql.get("login", { username });

    if (data) {
      ctx.body = {
        code: 2,
        msg: "账号已被注册",
      };
    } else {
      // 将用户名和密码 添加到数据库中
      await app.mysql.insert("login", {
        username,
        password,
        img,
        auth: "101,102,103",
        name: "张三",
      });

      ctx.body = {
        code: 1,
        msg: "注册成功",
      };
    }
  }

  // 获取用户信息
  async getUserInfo() {
    const { ctx, app } = this;
    const { authorization } = ctx.request.headers;
    try {
      const id = jwt.verify(authorization, "0311");
      const data = await app.mysql.get("login", { id });

      ctx.body = {
        code: 1,
        data,
      };
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: "登录已过期 请重新登录",
      };
    }
  }
}

module.exports = LoginController;
